package com.epalmpay.controller.apiweixin;


import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.service.weixin.ITokenService;
import com.epalmpay.service.weixin.IWxApiFindShopService;
import com.epalmpay.util.Response;
import com.epalmpay.util.StringUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;


/**
 * 
* 类名称：WxApiFindShopController.java
* 类描述： 发现门店接口
* @author zsj 
* 创建时间：2017年7月4日
 */
@Controller
@RequestMapping(value="/api/findshop")
public class WxApiFindShopController extends BaseController{
	
	@Resource
	private IWxApiFindShopService wxApiFindShopService;
	@Autowired
	private ITokenService tokenService;

	/**
	 * 优惠门店查询
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/finddiscountshoplist", method = RequestMethod.POST)
	@ResponseBody
	public Response findDiscountShopList(@RequestParam(required = true) String groupId,
							 @RequestParam(required = true) String memberId,
							 @RequestParam(required = false) String user_longitude,
							 @RequestParam(required = false) String user_latitude,
							 @RequestParam(required = false) Integer distance,
							 @RequestParam(required = false) String name,
							 @RequestParam(required = false) Integer goodCategory,
							 @RequestParam(required = false) Integer sortMethod,
							 @RequestParam(required = true) Integer pageNo,
							 @RequestParam(required = true) Integer pageSize) throws Exception {
		logBefore(logger, "优惠门店查询");
		Map<String, Object> map = null;

		try {
			map = wxApiFindShopService.findDiscountShopList(groupId,memberId,user_longitude,user_latitude,
					distance,name,goodCategory,sortMethod,pageNo,pageSize);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 门店优惠详情
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/shopdiscountdetail", method = RequestMethod.POST)
	@ResponseBody
	public Response shopDiscountDetail(@RequestParam(required = true) String groupId,
										 @RequestParam(required = true) String memberId,
										 @RequestParam(required = false) String user_longitude,
										 @RequestParam(required = false) String user_latitude,
										 @RequestParam(required = true) String shop_id) throws Exception {
		logBefore(logger, "门店优惠详情");
		Map<String, Object> map = null;

		try {
			map = wxApiFindShopService.getShopDiscountDetail(groupId,memberId,user_longitude,user_latitude,shop_id);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 优惠商品详情
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/discountgooddetail", method = RequestMethod.POST)
	@ResponseBody
	public Response discountGoodDetail(@RequestParam(required = true) String groupId,
									   @RequestParam(required = true) String memberId,
									   @RequestParam(required = false) String user_longitude,
									   @RequestParam(required = false) String user_latitude,
									   @RequestParam(required = true) String goodId,
									   @RequestParam(required = true) String shopId) throws Exception {
		logBefore(logger, "优惠商品详情");
		Map<String, Object> map = null;

		try {
			map = wxApiFindShopService.getDiscountGoodDetail(groupId,memberId,user_longitude,user_latitude,goodId,shopId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 商品图文详情
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goodpicturedetail", method = RequestMethod.POST)
	@ResponseBody
	public Response goodPictureDetail( @RequestParam(required = true) String memberId,
									   @RequestParam(required = true) String goodId) throws Exception {
		logBefore(logger, "商品图文详情");
		Map<String, Object> map = null;

		try {
			map = wxApiFindShopService.getGoodPictureDetail(memberId,goodId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 商品评价列表
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goodcommentlist", method = RequestMethod.POST)
	@ResponseBody
	public Response goodCommentList( @RequestParam(required = true) String goodId,
									 @RequestParam(required = false) Integer starLevel,
									 @RequestParam(required = false) Integer auditStatus,
									 @RequestParam(required = true) Integer pageNo,
									 @RequestParam(required = true) Integer pageSize) throws Exception {
		logBefore(logger, "商品图文详情");
		Map<String, Object> map = null;

		try {
			map = wxApiFindShopService.getGoodCommentList(goodId,auditStatus,starLevel,pageNo,pageSize);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 门店评价列表
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/shopcommentlist", method = RequestMethod.POST)
	@ResponseBody
	public Response shopcommentlist( @RequestParam(required = true) String shopId,
									 @RequestParam(required = false) Integer starLevel,
									 @RequestParam(required = false) Integer auditStatus,
									 @RequestParam(required = true) Integer pageNo,
									 @RequestParam(required = true) Integer pageSize) throws Exception {
		logBefore(logger, "门店评价列表");
		Map<String, Object> map = null;

		try {
			map = wxApiFindShopService.getShopCommentList(shopId,auditStatus,starLevel,pageNo,pageSize);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}


	/**
	 * 商品全部门店
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goodallshoplist", method = RequestMethod.POST)
	@ResponseBody
	public Response goodAllShopList( @RequestParam(required = true) String goodId,
									 @RequestParam(required = false) String user_longitude,
									 @RequestParam(required = false) String user_latitude
									 ) throws Exception {
		logBefore(logger, "商品图文详情");
		Map<String, Object> map = null;

		try {
			map = wxApiFindShopService.getGoodAllShopList(goodId,user_longitude,user_latitude);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	




}
